package com.zyw.dev.zyw.ui.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.zyw.dev.zyw.R;
import com.zyw.dev.zyw.adapter.SingleTypeViewAdapter;
import com.zyw.dev.zyw.dao.ZJWBeans;
import com.zyw.dev.zyw.db.DataBaseHealper;
import com.zyw.dev.zyw.mvp.model.ZJWDataBean;
import com.zyw.dev.zyw.utils.SMSTools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link ZJWTableFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ZJWTableFragment extends Fragment {

    public static final String SMS_DATA_LIST = "data_list";

    private View contentView;

    private ListView lv_table_view;

    private List<ZJWBeans> zjwBeansList;

    private SingleTypeViewAdapter singleTypeViewAdapter;

    private OnFragmentInteractionListener onFragmentInteractionListener;

    public ZJWTableFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ZJWTableFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ZJWTableFragment newInstance(List<ZJWBeans> zjwBeansList) {
        ZJWTableFragment fragment = new ZJWTableFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(SMS_DATA_LIST, (Serializable) zjwBeansList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            zjwBeansList = (List<ZJWBeans>) getArguments().getSerializable(SMS_DATA_LIST);
        }
        initData();
    }

    private void initData() {

    }

    private void initView() {
        lv_table_view = (ListView) contentView.findViewById(R.id.lv_table_view);


        singleTypeViewAdapter = new SingleTypeViewAdapter<ZJWBeans>(getContext(), zjwBeansList) {
            @Override
            public void bindToView(int position, View cellView, ZJWBeans zjwBeans) {

                if (null == zjwBeans) return;
                setViewText(cellView, R.id.tv_date, SMSTools.getDataFormat(zjwBeans.getDate()));
                setViewText(cellView, R.id.tv_density, zjwBeans.getDensity());
                setViewText(cellView, R.id.tv_wind, zjwBeans.getWind());
                setViewText(cellView, R.id.tv_light, zjwBeans.getLight());
                setViewText(cellView, R.id.tv_temp_rh, zjwBeans.getTempRH());

                setViewText(cellView, R.id.tv_temp, zjwBeans.getTemp());

                if (0 == (position) % 2) {
                    cellView.setBackgroundColor(Color.rgb(241, 241, 241));
//                    cellView.setBackground(getContext().getDrawable(R.drawable.bg_item_gray));
                } else {
                    cellView.setBackgroundColor(Color.WHITE);
                }
                ImageButton btn_delete = (ImageButton) getViewById(cellView, R.id.btn_delete);
                btn_delete.setVisibility(View.VISIBLE);
                btn_delete.setTag(position);
                btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = (int) v.getTag();
                        if (pos >= 0) {
                            showDeleteAlertDialog(dataList.get(pos));
                        }
                    }
                });
            }

            @Override
            public int layoutForCellView() {
                return R.layout.item_diagramme;
            }
        };

        lv_table_view.setAdapter(singleTypeViewAdapter);

    }

    private void delete() {
//        DataBaseHealper.newInstance().initDataBase();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        contentView = inflater.inflate(R.layout.fragment_zjwtable, container, false);
        initView();
        return contentView;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
//        if (mListener != null) {
//            mListener.onFragmentInteraction(uri);
//        }
    }

    private void showDeleteAlertDialog(final ZJWBeans zjwBeans) {

        if (null == zjwBeans) return;

        new AlertDialog.Builder(getContext()).setTitle("提示")

                .setMessage("确认删除 <"+SMSTools.getDataFormat(zjwBeans.getDate())+"> 这条数据?")

                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // TODO Auto-generated method stub
                        if (null != onFragmentInteractionListener){
                            onFragmentInteractionListener.onDelete(zjwBeans);
                        }
                    }

                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {//响应事件

                }

            }).show();//在按键响应事件中显示此对话框
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mListener = null;
    }

    public void setZJWBeanList(List<ZJWBeans> beanList) {
        this.zjwBeansList = beanList;
        singleTypeViewAdapter.setDataList(zjwBeansList);
    }

    public OnFragmentInteractionListener getOnFragmentInteractionListener() {
        return onFragmentInteractionListener;
    }

    public void setOnFragmentInteractionListener(OnFragmentInteractionListener onFragmentInteractionListener) {
        this.onFragmentInteractionListener = onFragmentInteractionListener;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onDelete(ZJWBeans zjwBeans);
    }
}
